import model.*;

import static java.lang.StrictMath.*;

/**
 * Created by pago0814 on 11.09.2014.
 */
public class HockeyistsMethods {

    public static void breakHock(Hockeyist self, Move move){
        if(move.getSpeedUp()>=0.2D)
            move.setSpeedUp(-1.0);
    }

    public static Hockeyist getNearestOpponent(double x, double y, World world) {
        Hockeyist nearestOpponent = null;
        double nearestOpponentRange = 0.0D;

        for (Hockeyist hockeyist : world.getHockeyists()) {
            if (hockeyist.isTeammate() || hockeyist.getType() == HockeyistType.GOALIE
                    || hockeyist.getState() == HockeyistState.KNOCKED_DOWN
                    || hockeyist.getState() == HockeyistState.RESTING) {
                continue;
            }

            double opponentRange = hypot(x - hockeyist.getX(), y - hockeyist.getY());

            if (nearestOpponent == null || opponentRange < nearestOpponentRange) {
                nearestOpponent = hockeyist;
                nearestOpponentRange = opponentRange;
            }
        }

        return nearestOpponent;
    }
    public static void aimAndSwing(Hockeyist self,Game game, Move move){
        double targetY;
        if (self.getY() >= Sync.gatesCenterY)
            targetY = Sync.gatesCenterY - (0.5D * game.getGoalNetHeight());
        else
            targetY = Sync.gatesCenterY + (0.5D * game.getGoalNetHeight());
        double angleToNet = self.getAngleTo(Sync.gatesCenterX, targetY);
        move.setTurn(angleToNet);
        if (abs(angleToNet) < Sync.STRIKE_ANGLE) {
            move.setAction(ActionType.SWING);
        }
    }
    public static void goToStrikePos(Hockeyist self, World world, Move move){
        double currentSpeed;
        if((Math.abs(self.getX()-Sync.gatesCenterX))<world.getWidth()/2)
            currentSpeed=0.8D;
        else
            currentSpeed=1.0D;
        if(onStrikePos(self))
             breakHock(self,move);
        else {
            double angle;
            Player oppl= world.getOpponentPlayer();
            if(!Sync.gatesRightSide) {
                if (self.getY() <= Sync.gatesCenterY) {
                    angle = self.getAngleTo(Sync.gatesCenterX + 180D, oppl.getNetTop());
                } else {
                    angle = self.getAngleTo(Sync.gatesCenterX + 180D, oppl.getNetBottom());
                }
            }else{
                if (self.getY() <= Sync.gatesCenterY) {
                    angle = self.getAngleTo(Sync.gatesCenterX - 180D, oppl.getNetTop());
                } else {
                    angle = self.getAngleTo(Sync.gatesCenterX - 180D, oppl.getNetBottom());
                }
            }
            move.setTurn(angle);
            move.setSpeedUp(currentSpeed);
        }
    }
    public static boolean onStrikePos(Hockeyist self){
        if(!Sync.gatesRightSide) {
            if (self.getX() >= Sync.gatesCenterX + 50D &&
                    self.getX() <= Sync.gatesCenterX + 400D &&
                    self.getY() >= Sync.gatesCenterY - 250D &&
                    self.getX() <= Sync.gatesCenterY + 250D)
                return true;
            else
                return false;
        }else{
            if (self.getX() <= Sync.gatesCenterX - 50D &&
                    self.getX() >= Sync.gatesCenterX - 400D &&
                    self.getY() >= Sync.gatesCenterY - 250D &&
                    self.getX() <= Sync.gatesCenterY + 250D)
                return true;
            else
                return false;
        }
    }
    public static boolean targetLooksAtMe(Hockeyist self, Unit target){
        if(Math.abs(target.getAngleTo(self))<=Sync.STRIKE_ANGLE)
            return true;
        else
            return false;
    }
    public static void lookAtPuck(Hockeyist self, World world, Move move){
        double angleToPuck = self.getAngleTo(world.getPuck());
        move.setTurn(angleToPuck);
    }
    public static Hockeyist getAttackerFriend(Hockeyist self, World world, Game game, Move move){
        for(Hockeyist hck: world.getHockeyists()) {
            if(hck.isTeammate()){
                if(hck!=self && hck.getType()!= HockeyistType.GOALIE){
                    return hck;
                }
            }
        }
        return null;
    }
    public static void rotateAndPassToTarget(Hockeyist self, Unit target, Move move){
        double angle = self.getAngleTo(target);
        move.setTurn(angle);
        if (abs(angle) < 0.1D) {
            move.setPassPower(0.8D);
            move.setPassAngle(angle);
            move.setAction(ActionType.PASS);
        }
    }
    public static void moveForSavinPuck(Hockeyist self, World world, Game game, Move move){
        if(self.getY()>=Sync.gatesCenterY)
            move.setTurn(2.0D * PI / 180.0D);
        else
            move.setTurn(-2.0D * PI / 180.0D);
        move.setSpeedUp(1.0D);
    }
    public static boolean enemyesBehind(Hockeyist self, World world, Game game, Move move){
        double myX=self.getX();
        for(Hockeyist hck: world.getHockeyists()){
            if(!hck.isTeammate() && hck.getType()!=HockeyistType.GOALIE){
                if(Math.abs(Sync.gatesCenterX-hck.getX())<Math.abs(Sync.gatesCenterX-myX))
                    return false;
            }
        }
        return true;
    }
}
